﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace _12_Marshaling
{
    internal delegate bool CallBackHandler(int hwnd, int param);

    class Program
    {
        private static int count = 0;
        //[DllImport("user32.dll", CharSet = CharSet.None)]
        //public static extern int MessageBoxA(int hWnd, string text, string caption, uint type);

        //PreserveSig = false - ignore error
        [DllImport("user32.dll", EntryPoint = "MessageBox", ExactSpelling = false, PreserveSig = false)]
        public static extern int Message(int hWnd, string text, string caption, uint type);

        [DllImport("msvcrt.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern int printf(string format, int i, double d);

        [DllImport("msvcrt.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern int printf(string format, int i, double d, string s);

        [DllImport("user32.dll")]
        public static extern int EnumWindows(CallBackHandler handler, int param);

        [DllImport("user32.dll")]
        public static extern int GetWindowText(int hWnd, StringBuilder title, int size);

        static bool CallBackFunction(int hWnd, int param)
        {
            StringBuilder title = new StringBuilder();
            GetWindowText(hWnd, title, 255);
            Console.WriteLine("HWND : {0}; Title: {1}", hWnd, title);
            return true;
        }

        static void Main(string[] args)
        {
            //MessageBoxA(0, "Hello, Marshaling", "Window", 0);
            //MessageBoxA(0, "Привіт, Маршалінг", "Вікно", 0);

            //Message(0, "Hello", "Window", 2);

            //printf("i = %d; d = %f\n", 5, 5.7);

            //printf("i = %d; d = %f; s = %s\n", 5, 5.7, "hello");

            CallBackHandler handler = CallBackFunction;
            EnumWindows(handler, 0);
            Console.WriteLine("count: {0}", count);
        }
    }
}
